/*
 * This packet is the response from the server to the client for the security
 * level setting request.
 *
 * @author Jonathan Walsh (jwalsh8484@gmail.com)
 */

package egs.packets;

import java.nio.ByteBuffer;

public class MoveSendPacket extends Packet {

    int board[][];
    short board_width;
    short board_height;

    public MoveSendPacket( short game_name, short game_version,
                           int board[][] ) {
        
        /*Set up the header*/
        message_type = MSG_MOVE_SEND;
        this.game_name = game_name;
        this.game_version = game_version;
        
        this.board_height = (short)board.length;
        this.board_width = (short)board[0].length;
        
        this.board = board;
        
        /*data headers and the board itself*/
        data_area_size = 4 + 4 + (board_width * board_height * 4);
    }
    
    public MoveSendPacket( byte[] raw ) {
        ByteBuffer buf = setHeaderNew( raw );
        
        board_width = buf.getShort();
        board_height = buf.getShort();
        
        int res = buf.getInt();
        
        board = new int[board_width][board_height];
        
        for ( int h = 0; h < board_height; h++ ) {
            for ( int w = 0; w < board_width; w++ ) {
                board[h][w] = buf.getInt();
            }
        }
    }
    
    public byte[] getRaw() {
        ByteBuffer buf = makeBufferWithHeader( data_area_size );
        
        buf.putShort( board_width );
        buf.putShort( board_height );
        
        buf.putInt( 0 );
        
        for ( int h = 0; h < board_height; h++ ) {
            for ( int w = 0; w < board_width; w++ ) {
                buf.putInt( board[h][w] );
            }
        }
        
        return buf.array();
    }
    
    
    public String toString() {
        String ret = super.toString();
        ret = ret + "\nboard_width="+board_width+" board_height="+board_height;
        ret = ret + "\nzero=0";
        
        for ( int h = 0; h < board_height; h++ ) {
            String line = "";
            for ( int w = 0; w < board_width; w++ ) {
                line = line + "  " + board[h][w];
            }
            ret = ret + "\n" + line;
        }
        return ret;
    }
    
    public int[][] get_board() { return board; }
    public short get_board_width() { return board_width; }
    public short get_board_height() { return board_height; }
}
